package controller

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/memberGrpc/fjmMessage"
	"context"
	"fmt"
	"gowork/memberGrpc/model"
	"encoding/json"
	"gowork/memberGrpc/fjmClient/util"
)

type MemberGrpcController struct{
    Ctx  iris.Context
}

func (c *MemberGrpcController) PostGrpcinsert() mvc.Result {

	// name := c.Ctx.FormValue("name")
	// info := c.Ctx.FormValue("info")
	//fmt.Println(name)

	var mb []*model.Member
	if err := c.Ctx.ReadJSON(&mb); err == nil{
		if data, derr := json.Marshal(&mb); derr == nil{
			var fmrr = fjmMessage.MemberListResponseOrRequest{
				MemberListStr:string(data),
			}
			res, err := util.Cli.InsertAll(context.TODO(), &fmrr)
			if err != nil {
				fmt.Println(err)
				return mvc.Response{
					Object : res,
				}
			}

			return mvc.Response{
				Object : res,
			}
		}
	}else{
		fmt.Println(err)
	}

	return mvc.Response{
		Object : map[string]string{
			"message":"解析失败",
			"code":"400",
		},
	}

	// var fmrr = fjmMessage.MemberListResponseOrRequest{
	// 	MemberListStr:info,
	// 	MemberListStr:info,
	// }

	// res, err := cli.InsertAll(context.TODO(), &fmrr)
	// if err != nil {
	// 	fmt.Println(err)
	// 	return mvc.Response{
	// 		Object : res,
	// 	}
	// }
	
	// return mvc.Response{
    //     Object : res,
    // }
}


func (c *MemberGrpcController) PostGrpcquery() mvc.Result {

	var em = fjmMessage.Empty{}

	res, err := util.Cli.QueryAllInfo(context.TODO(), &em)
	if err != nil {
		fmt.Println(err)
		return mvc.Response{
			Object : map[string]string{
				"message":"调用方法失败",
				"code":"400",
			},
		}
	}

	var mmm []*model.Member
	if uerr := json.Unmarshal([]byte(res.MemberListStr), &mmm); uerr == nil{
		return mvc.Response{
			Object : mmm,
		}

	}else{
		return mvc.Response{
			Object : map[string]string{
				"message":"解析失败",
				"code":"400",
			},
		}
	}
}


